
import keypress from 'keypress';
import { ControlCenter } from './control-center.js';

const music = '鸟之诗'; // 歌曲名，需要在 source/music 文件夹中有对应文件
const instrument = 'piano'; // 乐器，目前只支持 piano

const control = new ControlCenter(music, instrument);
control.load();

const isAuto = process.argv[2] === '--auto';
const timeGap = Number(process.argv[3]) || 200;

console.log(`播放模式：${isAuto ? '自动' : '手动'} ${isAuto ? `; 音符延时：${timeGap} ms` : ''}`);

if (isAuto) {
    const intervalID = setInterval(() => {
        const finished = control.next();
        if (finished) {
            console.log('\n\n\n歌曲表演结束，欢迎下次来玩！');
            clearInterval(intervalID);
        }
    }, timeGap);
    
} else {
    keypress(process.stdin);
    // 自己控制节奏：
    process.stdin.on('keypress', (ch) => {
        if(ch !== '\n') {
            return;
        }
        const finished = control.next();
        if (finished) {
            console.log('\n\n\n歌曲表演结束，欢迎下次来玩！');
            process.exit(0);
            return;
        }
    });
}
